// @ts-ignore
import request from '@/api/request.js'

// 地址接口类型定义
export interface AddressData {
  name: string
  area: string
  phone: string
  otherName: string
  isDefault: number
}

export interface AddressResponse {
  id: number
  name: string
  area: string
  phone: string
  otherName: string
  isDefault: number
  userId?: number
  createTime?: string
  updateTime?: string
}

export interface PageParams {
  page: number
  size: number
}

export interface PageResponse<T> {
  total: number
  records: T[]
  size: number
  current: number
}

// 后端API响应结构
export interface ApiResponse<T> {
  code: number
  message: string
  data: T
}

// 创建地址
export async function createAddress(data: AddressData): Promise<ApiResponse<any>> {
  return await request.post('/address', data)
}

// 分页查询地址
export async function getAddressPage(params: PageParams): Promise<ApiResponse<PageResponse<AddressResponse>>> {
  return await request.post('/address/page', params)
}

// 更新地址
export async function updateAddress(id: number, data: AddressData): Promise<ApiResponse<any>> {
  return await request.put(`/address/${id}`, data)
}

// 获取单个地址
export async function getAddress(id: number): Promise<ApiResponse<AddressResponse>> {
  return await request.get(`/address/${id}`)
}

// 删除地址
export async function deleteAddress(id: number): Promise<ApiResponse<any>> {
  return await request.delete(`/address/${id}`)
}
